

import {
  getOrderDetail, cancelOrder, buy, confirmDelivery,
  confirmReturn, payOrder, renewal
} from '../../services/order';
Page({
  data: {
    orderInfo: {},
    orderId: 0,
    billLi: [
      {
        type: 0,
        money: 0,
        expect: 3,
        date: '2020-12-10',
        status: 0,
      },
      {
        type: 1,
        money: 0,
        expect: 3,
        date: '2020-12-10',
        status: 1,
      },
      {
        type: 2,
        money: 0,
        expect: 3,
        date: '2020-12-10',
        status: 2,
      },
    ],
  },
  onShow(option) {
    my.setNavigationBar({
      title: '账单分期',
      backgroundColor: '#4BA3F9'
    })
  },
  onLoad(event) {
    this.setData({ orderId: event.orderId })
    this.getOrderDetail()
  },
  onSubmit(e) {
    my.alert({
      content: `数据：${JSON.stringify(e.detail.value)}`,
    });
  },
  onReset() {
  },
  filter() {

  },
  regionPicker() {
    my.regionPicker({
      mergeOptions: {},
      success: (res) => {
        my.alert({
          title: 'regionPicker response: ' + JSON.stringify(res),
        })
      },
    });
  },
  getOrderDetail() {
    getOrderDetail({ orderId: this.data.orderId }).then(res => {
      if (res.code == "000000") {
        let IsCanPay = false
        if (res.data.orderStageList != undefined) {
          res.data.orderStageList.forEach(element => {
            let statusName = ''
            // 1待支付 2无 3已支付 4逾期中 5取消 6退款失败 7退款成功）
            switch (element.status) {
              case 1:
                statusName = '待支付'
                IsCanPay = true
                break;
              case 2:
                statusName = '无'
                break;
              case 3:
                statusName = '已支付'
                break;
              case 4:
                statusName = '逾期中'
                IsCanPay = true
                break;
              case 5:
                statusName = '取消'
                break;
              case 6:
                statusName = '退款失败'
                break;
              case 7:
                statusName = '退款成功'
                break;
              default:
                break;
            }
            element.statusName = statusName
          });
        }
        res.data.IsCanPay = IsCanPay
        this.setData({ orderInfo: res.data })
      }
    })
  },
  checkorer(event) {
    let data = this.data.orderInfo
    if (data.orderStageList[event.target.dataset.index].status == 1 || data.orderStageList[event.target.dataset.index].status == 4) {
      if (data.orderStageList[event.target.dataset.index].active) {
        data.orderStageList[event.target.dataset.index].active = false
      } else {
        data.orderStageList[event.target.dataset.index].active = true
      }
      this.setData({ orderInfo: data })
    }
  },
  payOrder() {
    let orderID = this.data.orderInfo.orderId
    let orderStageIds = ''
    this.data.orderInfo.orderStageList.forEach(a => {
      if (a.active) {
        orderStageIds += a.id + ','
      }
    })
    if (orderStageIds.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '请选择要支付的分期账单',
        duration: 2000,
      });
      return
    }
    let params = {
      orderId: orderID,
      orderStageIds: orderStageIds.substring(0, orderStageIds.lastIndexOf(','))
    }
    console.log(params)
    payOrder(params).then(res2 => {
      if (res2.code == "000000") {
        let data = res2.data
        if (data.payType == 0) {
          my.showToast({
            type: 'fail',
            content: data.payData,
            duration: 2000,
          });
          return
        } else if (data.payType == 1) {
          my.tradePay({
            tradeNO: data.payData,
            success: function (res3) {
              console.log(res3)
              // let resultAli = JSON.stringify(res3)
              let resultAli = res3
              if (resultAli.resultCode == "9000") {
                this.onLoad()
              } else {
                my.showToast({
                  type: 'fail',
                  content: '支付失败',
                  duration: 2000,
                });
              }
            },
            fail: function (res) {
              console.log(res)
              // my.alert({
              //   content: JSON.stringify(res),
              // });
            },
          });
        }
        else if (data.payType == 2) {
          my.tradePay({
            // 调用资金冻结接口（alipay.fund.auth.order.app.freeze），获取资金授权参数
            orderStr: data.payData,
            success: (res3) => {

              console.log(res3)
              // let resultAli = JSON.stringify(res3)
              let resultAli = res3
              if (resultAli.resultCode == "9000") {
                this.onLoad()
              } else {
                my.showToast({
                  type: 'fail',
                  content: '支付失败',
                  duration: 2000,
                });
              }
            },
            fail: (res) => {
              console.log(res)
              // my.alert({
              //   content: JSON.stringify(res),
              // });
            }
          });
        }
      }
    })
  },
});
